package com.itheima.health.controller;

import com.itheima.health.common.MessageConst;
import com.itheima.health.entity.PageResult;
import com.itheima.health.entity.QueryPageBean;
import com.itheima.health.entity.Result;
import com.itheima.health.pojo.Menu;
import com.itheima.health.pojo.Permission;
import com.itheima.health.pojo.Role;
import com.itheima.health.pojo.User;
import com.itheima.health.service.RoleService;
import com.itheima.health.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;


import java.util.List;

/**
 * @author zhangmeng
 * @description
 * @date 2019/9/6
 **/
@RestController
@RequestMapping("/user")
@Slf4j
public class UserController {
    @DubboReference
    private UserService userService;
    @DubboReference
    private RoleService roleService;
    @GetMapping("/getUsername")
    public Result getUsername() {
        Object principal = SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        if (null != principal && principal instanceof org.springframework.security.core.userdetails.User) {
            org.springframework.security.core.userdetails.User user = (org.springframework.security.core.userdetails.User) principal;
            return new Result(true, MessageConst.GET_USERNAME_SUCCESS, user.getUsername());
        }
        return new Result(false, MessageConst.GET_USERNAME_FAIL);
    }

    /**
     * 动态菜单展示
     * @return
     */
    @GetMapping("/getMenuDisplay")
    public Result getMenuDisplay(){

        Object principal = SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        if (null != principal && principal instanceof org.springframework.security.core.userdetails.User) {
            org.springframework.security.core.userdetails.User user = (org.springframework.security.core.userdetails.User) principal;
//          获取当前用户菜单信息
           List<Menu> menus= userService.findByName(user.getUsername());
            return new Result(true, MessageConst.GET_MENU_SUCCESS, menus);
        }


        return new Result(false,MessageConst.GET_MENU_FAIL);
    }
    @RequestMapping("/loginSuccess")
    public  Result loginSuccess(){
        return new Result(true, MessageConst.LOGIN_SUCCESS);
    }
    @RequestMapping("/loginFail")
    public Result loginFail(){
        return new Result(false,"登录失败");
    }
    @RequestMapping("/findPage")
    public Result findPage(QueryPageBean queryPageBean){
        PageResult pageResult=userService.findPage(queryPageBean);
        return new Result(true,"角色查询成功",pageResult);
    }
    @RequestMapping("/findById")
    public Result findById(Integer id){
        User user=userService.findById(id);
        return new Result(true,"查询权限成功",user);
    }
    @RequestMapping("/findAll")
    public Result findAll(){
        List<Role> permissions= roleService.findAll();
        return new Result(true,"查询全部成功",permissions);
    }
    @RequestMapping("/findRoleIdsByUserId")
    public Result findRoleIdsByUserId(Integer id){
        Integer[] roleIds=roleService.findRoleIdsByUserId(id);
        return new Result(true,"回显权限id成功",roleIds);
    }
    @RequestMapping("/add")
    public Result add(Integer[] roleIds, @RequestBody User user){
        user.setPassword(new BCryptPasswordEncoder().encode(user.getPassword()));
        userService.add(roleIds,user);
        return new Result(true,"添加成功");
    }
    @RequestMapping("/edit")
    public Result edit(Integer[] roleIds, @RequestBody User user){
        userService.edit(roleIds,user);
        return new Result(true,"修改成功");
    }
    @RequestMapping("/del")
    public Result del(Integer id){
        userService.del(id);
        return new Result(true,"删除成功");
    }
}
